import { http } from "@/utils/http";
import util from "xgplayer/es/utils/util";
import checkIsFunction = util.checkIsFunction;

type Result = {
  success: boolean;
  data?: Array<any>;
};

type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};

/** 获取系统管理-用户管理列表 */
export const getUserList = (data?: object,method?:'get') => {
  let path = getnewUrl("/user",data,method)
  return http.request<ResultTable>(method, path, { data });
};
/** 重置他人密码 */
export const resetUserInfo = (data?: object,method?:'post') => {
  let path = getnewUrl("/user/resetInfo",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const batchSetIpList = (data?: object,method?:'post') => {
  let path = getnewUrl("/user/setIp",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const ruleSeting = (data?: object,method?:'get') => {
  let path = "/rule_set"
  return http.request<ResultTable>(method, path, { data });
};

/** 系统管理-用户管理-获取所有角色列表 */
export const getAllRoleList = () => {
  return http.request<Result>("get", "/role",{limit:1000});
};

/** 系统管理-用户管理-根据userId，获取对应角色id列表（userId：用户id） */
export const getRoleIds = (data?: object) => {
  return http.request<Result>("post", "/list-role-ids", { data });
};

/**
 * 拼装新的方法
 * @param path
 * @param data
 * @param method
 */
function getnewUrl(path,data?: object,method?:'get'){
  if(method ==undefined)method = 'get'
  if(method =='put' || method =='delete'){
    path = path + '/'+data.id
  }else if(method == 'get' && data !==undefined){
    const removeEmpty = obj => Object.keys(obj)
      .filter(key => obj[key] !== undefined && obj[key] !== null && obj[key] !== '')
      .reduce((acc, key) => {
        acc[key] = obj[key];
        return acc;
      }, {});
    data = removeEmpty(data);
    const query = new URLSearchParams(data).toString();
    path = `${path}?${query}`;
  }

  return path;
}
/** 获取系统管理-角色管理列表 */
export const getRoleList = (data?: object,method?:'get') => {
  let path = getnewUrl("/role",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getShopList = (data?: object,method?:'get') => {
  let path = getnewUrl("/erp_store",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getShopGroupList = (data?: object,method?:'get') => {
  let path = getnewUrl("/shop_group",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getRateList = (data?: object,method?:'get') => {
  let path = getnewUrl("/system_rate",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getCaseList = (data?: object,method?:'get') => {
  let path = getnewUrl("/system_case",data,method)
  return http.request<ResultTable>(method, path, { data });
};
//一键摧毁系统
export const destorySystem = (data?: object,method?:'post') => {
  let path = getnewUrl("/truncate_system",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const deleteCaseItem = (data?: object,method?:'post') => {
  let path = getnewUrl("/system_case/delete_item",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getExpressFeeList = (data?: object,method?:'get') => {
  let path = getnewUrl("/express_fee",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const importExpressFeeList = (data?: object,method?:'get') => {
  let path = getnewUrl("/express_fee/import",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const getDictValueList = (data?: object,method?:'get') => {
  let path = getnewUrl("/dicvalues",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getDictCateList = (data?: object,method?:'get') => {
  let path = getnewUrl("/dictionary",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const importRateList = (data?: object,method?:'get') => {
  let path = getnewUrl("/system_rate/import",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const importPerformanceList = (data?: object,method?:'get') => {
  let path = getnewUrl("/performance/import",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const performanceController = (data?: object,method?:'get') => {
  let path = getnewUrl("/performance",data,method)
  return http.request<Result>(method, path, { data })
};

export const updatePerformanceTelConnection = (id: number, data?: object) => {
  let path = `/performance/${id}/tel-connection`
  return http.request<Result>('put', path, { data })
};
/** 获取系统管理-菜单管理列表 */
export const getMenuList = (data?: object,method?:'get') => {
  let path = getnewUrl("/menu",data,method)
  return http.request<Result>(method, path, { data });
};

/** 获取系统管理-部门管理列表 */
export const getDeptList = (data?: object,method?:'get') => {
  let path = getnewUrl("/dept",data,method)
  return http.request<Result>(method, path, { data })
};
/** 获取系统监控-在线用户列表 */
export const getOnlineLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/online-logs", { data });
};

/** 获取系统监控-登录日志列表 */
export const getLoginLogsList = (data?: object,method?:'get') => {
  let path = getnewUrl("/login-logs",data,method)
  return http.request<ResultTable>(method, path, { data });
};

/** 获取系统监控-操作日志列表 */
export const getOperationLogsList = (data?: object,method?:'get') => {
  let path = getnewUrl("/operation-logs",data,method)
  return http.request<ResultTable>(method, path, { data });
};
/** 获取系统监控-系统日志列表 */
export const getSystemLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/system-logs", { data });
};

/** 获取系统监控-系统日志-根据 id 查日志详情 */
export const getSystemLogsDetail = (data?: object) => {
  return http.request<Result>("post", "/system-logs-detail", { data });
};
export const getTodo = (data?: object) => {
  return http.request<Result>("get", "/mine-todo?time="+data.time, { data });
};

/** 获取角色管理-权限-菜单权限 */
export const getRoleMenu = (data?: object) => {
  return http.request<Result>("post", "/role-menu", { data });
};

/** 获取角色管理-权限-菜单权限-根据角色 id 查对应菜单 */
export const getRoleMenuIds = (data?: object) => {
  return http.request<Result>("get", "/role-menu-ids?id="+data.id, { data });
};
export const setRoleMenuIds = (data?: object) => {
  return http.request<Result>("post", "/role-menu-ids", { data });
};
export const getPurchaseUserList = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase_user",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getPurchaseEntityList = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase_entity",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getSystemViewList = (data?: object,method?:'get') => {
  let path = getnewUrl("/system_view",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getOrderList = (data?: object,method?:'get') => {
  let path = getnewUrl("/orders",data,method)
  return http.request(method, path, { data });
};
export const getOrderDetail = (data?: object,method?:'get') => {
  let path = getnewUrl("/orders/"+data?.order_number,data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getOrdersPreList = (data?: object,method?:'get') => {
  let path = getnewUrl("/orders-pre",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getAdminNoticeList = (data?: object,method?:'get') => {
  let path = getnewUrl("/admin-notice",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const getOrdersAfterList = (data?: object,method?:'get') => {
  let path = getnewUrl("/orders-after",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const getOrdersTiaojiaList = (data?: object,method?:'get') => {
  let path = getnewUrl("/orders-adjust",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getOrdersCaijiList = (data?: object,method?:'get') => {
  let path = getnewUrl("/entity-gather/tolists",data,method)
  // let path = getnewUrl("/purchase/tolists",data,method) //TODO
  return http.request<ResultTable>(method, path, { data });
};
export const EntityGatherController = (data?: object,method?:'get') => {
  let path = getnewUrl("/entity-gather",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getOrderCaigouList = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase/tolists",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const getBaojiaItemsList = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase/calc_items",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const purchaseController = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase",data,method)
  return http.request<ResultTable>(method, path, { data });
};export const purchaseControllerSaveItem = (data?: object,method?:'post') => {
  let path = getnewUrl("/purchase/save_item",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const purchaseControllerAgain = (data?: object,method?:'post') => {
  let path = getnewUrl("/purchase/again",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const purchaseControllerAudit = (data?: object,method?:'post') => {
  let path = getnewUrl("/purchase/audit",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const productCategory = (data?: object,method?:'get') => {
  let path = getnewUrl("/product-category",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const LotteryLists = (data?: object,method?:'get') => {
  let path = getnewUrl("/lottery/lists",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const LotteryLogs = (data?: object,method?:'get') => {
  let path = getnewUrl("/lottery/logs",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const enableUser = (data?: object,method?:'put') => {
  let path = getnewUrl("/user/enable",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const PriceItemController = (data?: object,method?:'get') => {
  let path = getnewUrl("/price-items",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const ProductJinjiaController = (data?: object,method?:'get') => {
  let path = getnewUrl("/price-jinjia",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const ProductTiaojiaController = (data?: object,method?:'get') => {
  let path = getnewUrl("/price-tiaojia",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const businessAccount = (data?: object,method?:'get') => {
  let path = getnewUrl("/business_account",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const businessAccountDynamics = (data?: object,method?:'get') => {
  let path = getnewUrl("/business_account_dynamics",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const businessReturnLog = (data?: object,method?:'get') => {
  let path = getnewUrl("/business_return_log",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const ProductsController = (data?: object,method?:'get') => {
  let path = getnewUrl("/products",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const SearchViewController = (data?: object,method?:'get') => {
  let path = getnewUrl("/search-view",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const PurchaseItemsController = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase-items",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const WeightDiffController = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase1/weight-diff",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const ProductDiffController = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase1/price-diff",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const ProductPurchaseController = (data?: object,method?:'get') => {
  let path = getnewUrl("/purchase1/purchase-diff",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const OrderSmilarController = (data?: object,method?:'get') => {
  let path = getnewUrl("/order-smilar",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const UserFieldsController = (data?: object,method?:'get') => {
  let path = getnewUrl("/user-fields",data,method)
  return http.request<ResultTable>(method, path, { data });
};
export const OrderSmilarLists = (data?: object,method?:'get') => {
  let path = getnewUrl("order-smilar-lists",data,method)
  return http.request<ResultTable>(method, path, { data });
};
// Route::post('products-add-stock-log', [ProductsController::class,'addStockLog']);//缺货登录
// Route::post('products-clear-stock-log', [ProductsController::class,'clearStockLog']);//取消缺货登录
export const productsAddStockLog = (data?: object,method?:'get') => {
  let path = getnewUrl("/products-add-stock-log",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const productsClearStockLog = (data?: object,method?:'get') => {
  let path = getnewUrl("products-clear-stock-log",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const getOrderSta = (data?: object,method?:'get') => {
  delete data.limit;
  delete data.page;
  let path = getnewUrl("orders-sta",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const getOrderConfig = (data?: object,method?:'get') => {
  let path = getnewUrl("orders-config",data,method)
  return http.request<ResultTable>(method, path, { data });
};

export const getOrdersSynchronous = (data?: object,method?:'get') => {
  let path = getnewUrl("orders-synchronous",data,method)
  return http.request<ResultTable>(method, path, { data });
};

/** 文件上传 */
export const formUpload = data => {

  return http.request<ResultTable>("post","/user/resetInfo",{ data },{
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};
